<?php

class Admin_media extends Admin_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('Media_m');
        $this->_data['sidebar'] = 'admin_media';
    }

    function index() {
        is_admin();
        $this->list_category();
    }
    
   
    function list_media() {
        is_admin();
        $this->session->set_userdata('current_url', current_url());
       
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 5;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/admin_media/list_media' . '/' . $per_page . '/';
        $config['per_page'] = '5';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Media_m->count_media();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['images'] = $this->Media_m->get_all_medias($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Media_m->count_media();
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        
        $this->display_admin('admin/media/list_media');
    }

    function list_media_by_album($album_id) {
        is_admin();
        
        $per_page = $this->uri->segment(5);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(6);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/admin_media/list_media_by_album' . '/' . $album_id . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 6;
        $config['total_rows'] = $this->Media_m->count_media_by_album($album_id);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['images'] = $this->Media_m->get_all_medias_by_album($album_id, $config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Media_m->count_media_by_album($album_id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['album_name'] = $this->Media_m->get_name("albums", "id", $album_id)->title;
        $this->display_admin('admin/media/list_media');
    }

    
    function delete_media($id, $perpage, $offset) {
        is_admin();
        $media = $this->Media_m->get_detail_media($id);
        $album_id = $media['album_id'];
        
            $file_name = './assets/images/media/' . $media['file_path'];
            $thumbs_name = './assets/images/media/thumb/' . $media['file_path'];
            if ($media['file_path'] != null) {
                delete_images($file_name, $thumbs_name);
           
        	}

        $query = $this->Media_m->delete_media($id);
        if ($query)
            $this->session->set_flashdata('error', 'Xóa ảnh thành công');
        else
            $this->session->set_flashdata('error', 'Xóa ảnh thất bại');
        
        redirect('admin_media/list_media_by_album/'.$album_id);
    }

    function delete_multi_medias($perpage, $offset) {
        is_admin();
        $list = "";
        $adelete = $this->input->post('delete');
        $N = count($adelete);
        for ($i = 0; $i < $N; $i++) {
            $media = $this->Media_m->get_detail_media($adelete[$i]);
           
                $file_name = './assets/images/media/' . $media['file_path'];
                $thumbs_name = './assets/images/media/thumb/' . $media['file_path'];
                if ($media['file_path'] != null) {
                    delete_images($file_name, $thumbs_name);
                }
            
            if ($adelete[$i] != "")
                $list = $list . ',' . $adelete[$i];
        }
        $list = '(' . substr($list, 1) . ')';

        $query = $this->Media_m->delete_medias($list);
        if ($query)
            $this->session->set_flashdata('error', 'Xóa ảnh thành công');
        else
            $this->session->set_flashdata('error', 'Xóa ảnh thất bại');
        redirect('admin_media/list_media/' . $perpage . '/' . $offset);
    }

    function update_media($id, $kind, $perpage, $offset) {
        is_admin();
        $media = $this->_data['query'] = $this->Media_m->get_detail_media($id);
        
        $this->_data['per_page'] = $perpage;
        $this->_data['off_set'] = $offset;
        $this->_data['kind'] = $kind;
        
        $config = array(
                        array('field' => 'title',
                              'label' => 'Titre',
                              'rules' => 'required| trim| xss_clean'
                             ),
                             
                         array('field' => 'description',
                              'label' => 'Description',
                              'rules' => 'required| trim| xss_clean'
                             ),
                             
                        array('field' => 'video_link',
                              'label' => 'Video link',
                              'rules' => 'required'
                             )
                      
                       ) ;
        $this->form_validation->set_rules($config);
        
        
        if ($this->input->post('submit')) {
            if($this->form_validation->run()){
                
                $title = $this->input->post('title');
                $description = $this->input->post('description');
                $video_link = $this->input->post('video_link');
                $available = $this->input->post('available');
                $album = $this->input->post('album_id');
                
                
                $data = array('file_path' => $video_link,
                              'album_id'  => $album,
                               'title'    => $title,
                               'description' => $description,
                               'available' => $available 
                              );
                
                $query = $this->Media_m->update_media($id, $data);
                if ($query)
                    $this->session->set_flashdata('error', lang('update') .' '. lang('success'));
                else
                    $this->session->set_flashdata('error', lang('update').' ' . lang('fail'));
                redirect('admin_media/list_media/' . $kind . '/' . $perpage . '/' . $offset);   
            }else{
                $this->display_admin('admin/media/update_media');
            }
      
        } else {
            $this->display_admin('admin/media/update_media');
        }
    }
    
    function update_image($id, $kind, $perpage, $offset){
        is_admin();
        $media = $this->_data['query'] = $this->Media_m->get_detail_media($id);
        
        $this->_data['per_page'] = $perpage;
        $this->_data['off_set'] = $offset;
        $this->_data['kind'] = $kind;
        
        
        $config = array(
            array(
                'field' => 'title',
                'label' => lang('title'),
                'rules' => 'required'
            ),
            array(
                'field' => 'description',
                'label' => lang('description'),
                'rules' => 'required'
            )
        );               
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
                if($this->form_validation->run()){
                     $image_path = './assets/media/images';
                     $thumb_path = $image_path . '/thumbs';
                     $config = array(
                                    'allowed_types' => "jpg|jpeg|gif|png",
                                    'upload_path' => $image_path,
                                    'max_size' => 10000,
                                    'encrypt_name' => TRUE
                                     ); 
                     $this->load->library('upload', $config);
                     if (!$this->upload->do_upload()) {   
                         $data = array(
                            'title' => $this->input->post('title'),
                            'description' => $this->input->post('description'),
                            'album_id' => $this->input->post('album_id'),
                            );
                         $query = $this->Media_m->update_media($id, $data);
                         if ($query)
                            $this->session->set_flashdata('error', "L'image a été modifié !");
                            else
                            $this->session->set_flashdata('error', "L'image n'est pas modifié, veuillez reessayer !");
                        redirect('admin_media/list_media/' . $kind . '/' . $perpage . '/' . $offset);  
                     }else {
                         
                        $query = $this->Media_m->get_detail_media($id); 
                        if (!empty($query)) {
                            $info = $query;
                            if ($info['file_path'] != null) {
                                $image_to_delete = './assets/media/images/' . $info['file_path'];
                                $thumb_to_delete = './assets/media/images/thumbs/' . $info['file_path'];
                                delete_images($image_to_delete, $thumb_to_delete);
                            }
                        }
                     
                     $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    $data = array(
                        'title' => $this->input->post('title'),
                        'description' => $this->input->post('description'),
                        'album_id' => $this->input->post('album_id'),
                        'file_path' => $upload_info['file_name']
                     
                    );
                    $query = $this->Media_m->update_media($id, $data);
                    if ($query)
                        $this->session->set_flashdata('error', "L'article a Ã©tÃ© modifiÃ© !");
                    else
                        $this->session->set_flashdata('error', "L'article n'est pas modifiÃ©, veuillez reessayer !");
                    redirect('admin_media/list_media/' . $kind . '/' . $perpage . '/' . $offset);
                }
            }
        }
        
        $this->display_admin('admin/media/update_image');
     
    }

    function list_albums() {
        is_admin();
        $this->session->set_userdata('current_url', current_url());
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 4;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/admin_media/list_albums/' . $per_page . '/';
        $config['per_page'] = '6';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Media_m->count_album();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['albums'] = $this->Media_m->get_all_albums($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Media_m->count_album();
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_admin('admin/media/list_albums');
    }

    function add_album() {
        is_admin();
        
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Tên album',
                'rules' => 'required'
            ),
             array(
                'field' => 'category',
                'label' => 'Bộ sưu tập',
                'rules' => 'xss_clean'
            )
          
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'category_id'=> $this->input->post('category')
                   
                );
                $this->Media_m->add_album($data);
                $this->session->set_flashdata('error', 'Thêm album mới thành công');
                redirect('admin_media/list_album_by_category/'.$this->input->post('category'));
            }
            $this->display_admin('admin/media/add_album');
        }
        $this->display_admin('admin/media/add_album');
    }

   
    function delete_album($id) {
        is_admin();
        $cat_id = $this->Media_m->get_category_of_album($id)->category_id;
        $media = $this->Media_m->get_media_by_album($id);
        foreach ($media as $m) {
           
                $file_name = './assets/images/media/' . $m['file_path'];
                $thumbs_name = './assets/images/media/thumb/' . $m['file_path'];
                if ($m['file_path'] != null) {
                    delete_images($file_name, $thumbs_name);
                }
            
        }
        $query = $this->Media_m->delete_album($id);
        if ($query > 0) {
            $this->session->set_flashdata('error', 'Xóa album thành công');
        }
        
       
            redirect('admin_media/list_album_by_category/'.$cat_id);
   
    }
    

    function edit_album($id) {
        is_admin();
        $this->session->set_userdata('current_url', current_url());
        $query = $this->Media_m->get_album_by_id($id);
        $this->_data['query'] = $query;
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Tên album',
                'rules' => 'required'
            ),
             array(
                'field' => 'category',
                'label' => 'Bộ sưu tập',
                'rules' => 'xss_clean'
            )
            
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'category_id' => $this->input->post('category')
                );
                $this->Media_m->update_album($data, $id);
                $this->session->set_flashdata('error', "Sửa album thành công");
                redirect('admin_media/list_album_by_category/'.$this->input->post('category'));
            }
            $this->display_admin('admin/media/edit_album');
        }
        $this->display_admin('admin/media/edit_album');
    }

    //MULTI IMAGES
    function add_images() {
        is_admin();
        if ($this->input->post('submit')) {
           
                $image_path = './assets/images/media';
                $thumb_path = $image_path . '/thumb';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' => TRUE
                );
               $this->load->library('upload',$config);
                $this->load->library('Multi_upload'); 
                $this->upload->initialize($config);
                
                $files = $this->multi_upload->go_upload();
                
                if (!$files) {
                    $this->session->set_flashdata('error', $this->upload->display_errors());
                    redirect('admin_media/add_images');
                } else {
                    $this->load->library('image_lib');
                    for ($i = 0; $i < count($files); $i++) {
                        $config = array(
                            'source_image' => $files[$i]['file'], //get original image
                            'new_image' => $thumb_path, //save as new image //need to create thumbs first
                            'maintain_ratio' => true,
                            'width' => 120,
                            'height' => 80
                        );
                        $this->image_lib->initialize($config);  //load library
                        $this->image_lib->resize();

                        $config1 = array(
                            'source_image' => $files[$i]['file'], //get original image
                            'new_image' => $image_path, //save as new image //need to create thumbs first
                            'maintain_ratio' => true,
                            'width' => 1024,
                            'height' => 768
                        );
                        $this->image_lib->initialize($config1);  //load library
                        $this->image_lib->resize();

                        $query = $this->Media_m->add_media(array('file_path' => $files[$i]['name'], 'album_id'=>$this->input->post('album_id')));
                    }

                    $data = array('upload_data' => $files);
                    if ($query) {
                        $this->session->set_flashdata('error', 'Upload ảnh thành công');
                    } else {
                        $this->session->set_flashdata('error', 'Upload'.' ' . lang('fail'));
                    }
                    redirect('admin_media/list_media_by_album/'.$this->input->post('album_id'));
                }
           
        } else {
            $this->display_admin('admin/media/add_images');
        }
    } 
    
    
    //them moi bo suu tap
    //cua album anh
      function add_category() {
        is_admin();
        
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Tên bộ sưu tập',
                'rules' => 'required'
            )
          
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'name' => $this->input->post('title'),
                    'date_created' => now()
                   
                );
                $this->Media_m->add_category($data);
                $this->session->set_flashdata('error', 'Thêm bộ sưu tập mới thành công');
                redirect('admin_media/list_category');
            }
            $this->display_admin('admin/media/add_category');
        }
        $this->display_admin('admin/media/add_category');
    }
    
    function list_category() {
        is_admin();
        
//        $per_page = $this->uri->segment(3);
//        if ($per_page == "") {
//            $per_page = 4;
//        }
//        $off_set = $this->uri->segment(4);
//        if ($off_set == "") {
//            $off_set = 0;
//        }
//        $config['base_url'] = base_url() . 'index.php/admin_media/list_category/' . $per_page . '/';
//        $config['per_page'] = '6';
//        $config['uri_segment'] = 4;
//        $config['total_rows'] = $this->Media_m->count_category();
//        if ($off_set > 0 && $off_set == $config['total_rows']) {
//            $off_set = $off_set - $per_page;
//        }
        $this->_data['category'] = $this->Media_m->get_all_category();
//        $this->pagination->initialize($config);
//        $this->_data['pagination'] = $this->pagination->create_links();
//        $this->_data['total'] = $this->Media_m->count_category();
//        $this->_data['per_page'] = $per_page;
//        $this->_data['off_set'] = $off_set;
        $this->display_admin('admin/media/list_category');
    }
    
    function list_album_by_category($category_id) {
        is_admin();
        $per_page = $this->uri->segment(5);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(6);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/admin_media/list_album_by_category' . '/' . $category_id . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 6;
        $config['total_rows'] = $this->Media_m->count_album_by_category($category_id);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['albums'] = $this->Media_m->get_all_album_by_category($category_id, $config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['category_name'] = $this->Media_m->get_name("album_category", "id", $category_id)->name;
        
        $this->display_admin('admin/media/list_album_by_category');
    }
    
    function edit_category($id) {
        is_admin();
        
        $query = $this->Media_m->get_category_by_id($id);
        $this->_data['query'] = $query;
        $config = array(
            array(
                'field' => 'title',
                'label' => 'Tên bộ sưu tập',
                'rules' => 'required'
            )
            
            
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'name' => $this->input->post('title')
                    
                );
                $this->Media_m->update_category($data, $id);
                $this->session->set_flashdata('error', "Sửa bộ sưu tập thành công");
                redirect('admin_media/list_category');
            }
            $this->display_admin('admin/media/edit_category');
        }
        $this->display_admin('admin/media/edit_category');
    }
    
    function delete_category($id) {
        is_admin();
     
        $query = $this->Media_m->delete_category($id);
        if ($query > 0) {
            $this->session->set_flashdata('error', 'Xóa bộ sưu tập thành công');
        }
        redirect('admin_media/list_category');
    }
 
}

